package wifiunion.com.notice.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.smart.colorview.utils.ResUtils;


import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;

import wifiunion.com.notice.R;
import wifiunion.com.notice.entity.PushEntity;

/**
 * Created by Administrator on 2017/4/24.
 */

public class NoticeBackgroundAdapter extends RecyclerView.Adapter<NoticeBackgroundViewHolder> {

    private Context mContext;

    /**
     * item项的宽
     */
    private int mPerWidth;

    private int mScreenHeight;

    private ArrayList<PushEntity> mData = new ArrayList<>();


    private int circleSize;

    private int textColor;


    private int mAlpha;

   private DecimalFormat df;

    public NoticeBackgroundAdapter(Context context, ArrayList<PushEntity> data) {
        mContext = context;
        mData = data;
        mPerWidth = getPerSize();
        mScreenHeight = getScreenHeight();
        circleSize = mContext.getResources().getDimensionPixelSize(R.dimen.dp_138);
        df = new DecimalFormat("0.00");
    }


    @Override
    public NoticeBackgroundViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        RelativeLayout view;
        view = (RelativeLayout) LayoutInflater.from(parent.getContext()).inflate(R.layout.noticebackground_item, parent, false);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(mPerWidth, mScreenHeight);
        view.setLayoutParams(params);
        NoticeBackgroundViewHolder rv = new NoticeBackgroundViewHolder(view);
        return rv;
    }

    @Override
    public void onBindViewHolder(NoticeBackgroundViewHolder holder, int pos) {
        PushEntity pe = mData.get(pos);

        String picurl = pe.getPicUrl();
        int stranger = pe.getIsStranger();
        int alarmstatus = pe.getAlarmState();
        String name = pe.getName();
        String content = pe.getContent();
//        holder.mBgIv.setVisibility(View.GONE);
//        holder.mHeaderIv.setVisibility(View.GONE);
//        holder.mStatusIv.setVisibility(View.GONE);
//        holder.mNameTv.setVisibility(View.GONE);
//        holder.mNoticeTv.setVisibility(View.GONE);

        holder.mNameTv.setTextColor(textColor);
        holder.mNoticeTv.setTextColor(textColor);

//        int timeShow = pe.getIsShowTime();

        if (stranger == 0) {
            if (alarmstatus == 0) {
                holder.mBgIv.setVisibility(View.VISIBLE);
                holder.mHeaderIv.setVisibility(View.VISIBLE);
                holder.mStatusIv.setVisibility(View.VISIBLE);
                holder.mNameTv.setVisibility(View.VISIBLE);
                holder.mNoticeTv.setVisibility(View.VISIBLE);
//                holder.mBgIv.setImageResource(R.drawable.item_blue_bg);

                holder.mUpIv.setImageResource(R.drawable.blue_up);
                holder.mDownIv.setImageResource(R.drawable.blue_down);
                holder.mBgIv.setImageAlpha(mAlpha);
//                holder.mBgIv.setBackgroundColor(Color.parseColor(backGroundColor));
                //不告警
                Glide.with(mContext).load(R.drawable.noticebg_ren)
                        .placeholder(R.drawable.mainview_default_header).error(R.drawable.mainview_default_header)
                        .centerCrop().transform(new NoticeBackgroundGlideCircleHasBackgroudTransform(mContext, 1, circleSize)
                        /*new GlideCircleHasBackgroudTransform(mContext,1)*/).into(holder.mHeaderIv);
                holder.mStatusIv.setImageResource(R.drawable.status_normal);
                holder.mNameTv.setText(name);
                holder.mNoticeTv.setText(content);


            } else if (alarmstatus == 1) {
                holder.mBgIv.setVisibility(View.VISIBLE);
//                holder.mBgIv.setImageResource(R.drawable.item_red_bg);
                holder.mUpIv.setImageResource(R.drawable.red_up);
                holder.mDownIv.setImageResource(R.drawable.red_down);
                holder.mBgIv.setImageAlpha(mAlpha);
//                holder.mBgIv.setBackgroundColor(Color.parseColor(backGroundColor));
                holder.mHeaderIv.setVisibility(View.VISIBLE);
                holder.mStatusIv.setVisibility(View.VISIBLE);
                holder.mNameTv.setVisibility(View.VISIBLE);
                holder.mNoticeTv.setVisibility(View.VISIBLE);
                //告警
                Glide.with(mContext).load(R.drawable.noticebg_ren2)
                        .placeholder(R.drawable.mainview_default_header).error(R.drawable.mainview_default_header)
                        .centerCrop().transform(new NoticeBackgroundGlideCircleHasBackgroudTransform(mContext, 2, circleSize)
                        /*new GlideCircleHasBackgroudTransform(mContext,2)*/).into(holder.mHeaderIv);
                holder.mStatusIv.setImageResource(R.drawable.status_danger);
                holder.mNameTv.setText(name);
                holder.mNoticeTv.setText(content);
            } else {
                holder.mBgIv.setImageResource(0);
                holder.mBgIv.setVisibility(View.GONE);
                holder.mHeaderIv.setVisibility(View.GONE);
                holder.mStatusIv.setVisibility(View.GONE);
                holder.mNameTv.setVisibility(View.GONE);
                holder.mNoticeTv.setVisibility(View.GONE);
            }
        } else if (stranger == 1) {
            holder.mBgIv.setVisibility(View.VISIBLE);
//            holder.mBgIv.setImageResource(R.drawable.item_yellow_bg);
            holder.mUpIv.setImageResource(R.drawable.yellow_up);
            holder.mDownIv.setImageResource(R.drawable.yellow_down);
            holder.mBgIv.setImageAlpha(mAlpha);

//            holder.mBgIv.setBackgroundColor(Color.parseColor(backGroundColor));
            holder.mHeaderIv.setVisibility(View.VISIBLE);
            holder.mStatusIv.setVisibility(View.VISIBLE);
            holder.mNameTv.setVisibility(View.VISIBLE);
            holder.mNoticeTv.setVisibility(View.VISIBLE);
            //陌生人
            Glide.with(mContext).load(R.drawable.noticebg_ren3)
                    .placeholder(R.drawable.mainview_default_header).error(R.drawable.mainview_default_header)
                    .centerCrop().transform(new NoticeBackgroundGlideCircleHasBackgroudTransform(mContext, 3, circleSize)
                    /*new GlideCircleHasBackgroudTransform(mContext,3)*/).into(holder.mHeaderIv);
            holder.mStatusIv.setImageResource(R.drawable.status_unkonw);
            holder.mNameTv.setText(TextUtils.isEmpty(name) ? "未登记" : name);
            holder.mNoticeTv.setText(content);


        } else {
            holder.mBgIv.setVisibility(View.GONE);
            holder.mHeaderIv.setVisibility(View.GONE);
            holder.mStatusIv.setVisibility(View.GONE);
            holder.mNameTv.setVisibility(View.GONE);
            holder.mNoticeTv.setVisibility(View.GONE);
        }


//        if (pe.getIsShowTime() == 0) {
//            holder.timeRl.setVisibility(View.GONE);
//        } else {
//            holder.timeRl.setVisibility(View.VISIBLE);
//            holder.firstTv.setText("显示初次见面   " + pe.getFirstTime());
//            holder.lastTv.setText("上次见面时间   " + pe.getLastTime());
//        }


    }

    @Override
    public int getItemCount() {
        return mData.size();
    }


    /**
     * 屏宽
     *
     * @return
     */
    private int getScreenWidth() {
        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        Display dis = wm.getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        dis.getMetrics(metrics);
        return metrics.widthPixels;
    }

    /**
     * 屏高
     *
     * @return
     */
    private int getScreenHeight() {
//        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
//        Display dis = wm.getDefaultDisplay();
//        DisplayMetrics metrics = new DisplayMetrics();
//        dis.getMetrics(metrics);

        int height  = mContext.getResources().getDimensionPixelSize(R.dimen.dp_340);
        return  height;
//        return metrics.heightPixels;
    }

    /**
     * 设置文字颜色
     */
    public void setTextColor(int  selectPos)
    {
        selectPos = selectPos;
        textColor = mContext.getResources().getColor(ResUtils.getColorId(mContext, "smart_color_" + selectPos));

    }

    public void setBackGroundColor(int bgColor)
    {
        if(bgColor == 100)
        {
            mAlpha = 255;
        }
        else
        {
            int a = bgColor;
            int b = 100;

            double result = new BigDecimal((float)a / b).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            BigDecimal a1 = new BigDecimal(Double.toString((result)));
            BigDecimal b1 = new BigDecimal(Double.toString(255));
            mAlpha = a1.multiply(b1).intValue();

        }

    }

    /**
     * 每项的宽
     *
     * @return
     */
    private int getPerSize() {

        int wh  = mContext.getResources().getDimensionPixelSize(R.dimen.dp_480);

        return wh / 3;
    }
}
